package com.cgbl.cg.ui.project

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.ClassicCaseAdapter
import com.cgbl.cg.databinding.AppActivityProjectMenuBinding
import com.cgbl.cg.view.SelectCoursePop
import com.cgbl.cg.vm.project.ProjectMenuViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.ProjectBean

@Route(path = AppConstants.Router.PROJECT.A_PROJECT_MENU)
class ProjectMenuActivity : GLBaseActivity<AppActivityProjectMenuBinding, ProjectMenuViewModel>() {

    private lateinit var classicCaseAdapter: ClassicCaseAdapter
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_project_menu

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initAdapter()
    }

    override fun initData() {
        super.initData()
        viewModel.initData()
    }

    private fun initAdapter() {
        classicCaseAdapter = ClassicCaseAdapter()
        binding.recyclerView.apply {
            adapter = classicCaseAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.selectType.observe(this) {
            val selectCoursePop =
                SelectCoursePop(this, viewModel.typeName.get() ?: "", viewModel.list)
            XPopup.Builder(this)
                .atView(binding.clTitle)
                .asCustom(selectCoursePop)
                .show()

            selectCoursePop.setConfirmClickListener {
                selectCoursePop.dismiss()
                viewModel.typeName.set(it.name)
            }
        }

        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }

        binding.smartRefreshLayout.setOnLoadMoreListener {
            viewModel.load()
        }

        viewModel.uc.projectEvent.observe(this){
            if (viewModel.page == 1) {
                classicCaseAdapter.setNewInstance(it as MutableList<ProjectBean>?)
            }else{
                classicCaseAdapter.addData(it)
            }

        }

    }
}